/*
 *   Copyright (c) 2009-@year@. The GUITAR group at the University of
 *   Maryland.  Names of owners of this group may be obtained by sending an
 *   e-mail to atif@cs.umd.edu
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a
 *   copy of this software and associated  documentation files (the "Software"),
 *   to deal in the Software without restriction,  including without  limitation
 *   the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *   and or sell copies of the Software, and to permit persons to whom the
 *   Software is furnished to do so, subject to the following  conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial  portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT  LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO  EVENT SHALL
 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR  THE USE OR OTHER
 *   DEALINGS IN THE SOFTWARE.
 */
package edu.umd.cs.emut.executor.replayer;

import org.apache.log4j.Logger;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import edu.umd.cs.emut.ETool;
import edu.umd.cs.emut.XMLHandler;
import edu.umd.cs.emut.exception.EException;
import edu.umd.cs.emut.executor.EExecutionMonitor;
import edu.umd.cs.emut.executor.EExecutionStrategy;
import edu.umd.cs.emut.executor.Executor;
import edu.umd.cs.emut.executor.ExecutorBuilder;
import edu.umd.cs.emut.executor.WebMapUpdater;
import edu.umd.cs.emut.model.EApplication;
import edu.umd.cs.emut.model.WebApplication;
import edu.umd.cs.emut.model.xml.GUIMap;
import edu.umd.cs.emut.model.xml.TestCase;

/**
 * 
 * @author <a href="mailto:baonn@cs.umd.edu"> Bao N. Nguyen </a>
 * 
 */
public class WebReplayerTool extends ETool {

	/**
	 * 
	 */

	private static Logger LOGGER = Logger.getLogger(WebReplayerTool.class);;

	// Parameters for the replayer
	@Option(name = "-u", usage = "Root URL", aliases = "--url", required = true)
	String originURL = null;

	@Option(name = "-m", usage = "Map file to lookup components", aliases = "--map-file", required = true)
	String mapFile = null;

	@Option(name = "-t", usage = "Test case", aliases = "--testcase", required = true)
	String testcaseFile = null;

	@Option(name = "-om", usage = "Output map file to store new components", aliases = "--out-map-file", required = true)
	String outMapFile = null;

	/**
	 * @param args
	 */
	public WebReplayerTool(String[] args) {
		super(args);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.umd.cs.emut.ETool#execute()
	 */
	@Override
	public void execute() {
		CmdLineParser parser = new CmdLineParser(this);

		try {

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("START: " + this.getClass().getName());
			}

			parser.parseArgument(getArgs());
			EApplication application = new WebApplication(originURL);

			// Configure the replayer
			XMLHandler xmlHandler = new XMLHandler();
			GUIMap map = (GUIMap) xmlHandler.readObjFromFile(mapFile,
					GUIMap.class);
			TestCase testcase = (TestCase) xmlHandler.readObjFromFile(
					testcaseFile, TestCase.class);

			assert (testcase != null);
			assert (map != null);

			EExecutionStrategy replayStrategy = new WebReplaying(map, testcase);
			EExecutionMonitor mapUpdater = new WebMapUpdater(outMapFile);

			ExecutorBuilder eb = new ExecutorBuilder();
			eb = eb.setApplication(application).setStrategy(replayStrategy);
			// eb = eb.addMonitor(mapUpdater);

			Executor webReplayer = eb.build();
			// // Execute
			webReplayer.execute();
			printInfo();

			LOGGER.info("DONE");

		} catch (CmdLineException e) {
			LOGGER.error(e.getMessage());
		} catch (EException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 */
	private void printInfo() {
		LOGGER.info("Output mapping: " + outMapFile);
	}
}
